package job.Swing;


import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class AddUserView extends JFrame {
    private JComboBox<String> hotelComboBox;
    private JTextField nameField;
    private JTextField countryField;
    private JTextField checkInDateField;
    private JButton submitButton;
    private HotelController controller;

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    public AddUserView(HotelController controller) {
        this.controller = controller;

        setTitle("添加运动员");
        setSize(400, 250);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        initComponents();
        layoutComponents();
        registerListeners();
    }

    private void initComponents() {
        hotelComboBox = new JComboBox<>();
        nameField = new JTextField(20);
        countryField = new JTextField(20);
        checkInDateField = new JTextField(20);
        submitButton = new JButton("提交");

        ArrayList<String> hotels = controller.getService().getHotelNames();
        for (String hotel : hotels) {
            hotelComboBox.addItem(hotel);
        }
    }

    private void layoutComponents() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.anchor = GridBagConstraints.WEST;

        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(new JLabel("酒店名称："), gbc);
        gbc.gridx = 1;
        panel.add(hotelComboBox, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        panel.add(new JLabel("运动员姓名："), gbc);
        gbc.gridx = 1;
        panel.add(nameField, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        panel.add(new JLabel("运动员国籍："), gbc);
        gbc.gridx = 1;
        panel.add(countryField, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        panel.add(new JLabel("入住时间（yyyy-MM-dd）："), gbc);
        gbc.gridx = 1;
        panel.add(checkInDateField, gbc);

        gbc.gridx = 1;
        gbc.gridy++;
        gbc.anchor = GridBagConstraints.CENTER;
        panel.add(submitButton, gbc);

        add(panel);
    }

    private void registerListeners() {
        submitButton.addActionListener(e -> {
            String hotel = (String) hotelComboBox.getSelectedItem();
            String name = nameField.getText().trim();
            String country = countryField.getText().trim();
            String dateStr = checkInDateField.getText().trim();

            if (hotel == null || name.isEmpty() || country.isEmpty() || dateStr.isEmpty()) {
                JOptionPane.showMessageDialog(this, "请填写完整信息", "警告", JOptionPane.WARNING_MESSAGE);
                return;
            }

            try {
                Date date = DATE_FORMAT.parse(dateStr);
                boolean success = controller.getService().addUserToHousehold(hotel, name, country, date);
                if (success) {
                    JOptionPane.showMessageDialog(this, "添加成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                    controller.refresh();
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(this, "添加失败！", "失败", JOptionPane.ERROR_MESSAGE);
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "日期格式错误，应为 yyyy-MM-dd", "错误", JOptionPane.ERROR_MESSAGE);
            }
        });
    }
}
